package com.tomecode.tools.maven.settings.gui;

import java.io.File;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.layout.grouplayout.LayoutStyle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import com.tomecode.tools.maven.settings.SettingsFactory;
import com.tomecode.tools.maven.settings.config.Maven;
import com.tomecode.tools.maven.settings.gui.frm.FrmMirrors;
import com.tomecode.tools.maven.settings.gui.frm.FrmProfile;
import com.tomecode.tools.maven.settings.gui.frm.FrmProxy;
import com.tomecode.tools.maven.settings.gui.frm.FrmSelectPluginGroups;
import com.tomecode.tools.maven.settings.gui.frm.FrmSelectProfile;
import com.tomecode.tools.maven.settings.gui.frm.FrmServer;
import com.tomecode.tools.maven.settings.gui.frm.loading.FrmLoadingPluginGroups;
import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.GuiUtils;
import com.tomecode.tools.maven.settings.gui.utils.SWTResourceManager;
import com.tomecode.tools.maven.settings.model.Activation;
import com.tomecode.tools.maven.settings.model.Mirror;
import com.tomecode.tools.maven.settings.model.Profile;
import com.tomecode.tools.maven.settings.model.Proxy;
import com.tomecode.tools.maven.settings.model.Server;
import com.tomecode.tools.maven.settings.model.Settings;

/**
 * Root editor
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class EditorWindow {
	private Text textRepositoryPath;
	private Table tableServers;
	private Table tableMirrors;
	private Table tableProxies;
	private Label labelWarning;
	private Button btnSave;
	private List listActiveProfiles;
	private List listPluginGroups;
	private Table tableProperties;
	private Table tableRepositories;
	private Table tablePluginRepositories;
	private Text textProfileJDK;
	private List listProfiles;
	private Text textOsName;
	private Text textOsFamily;
	private Text textOsArch;
	private Text textOsVersion;
	private Shell shlMavenSettingsEditor;
	private Display display;

	private Settings newSettings;
	private Label labelHelpUsePluginRegistry;

	/**
	 * Constructor
	 */
	public EditorWindow() {
		this.display = Display.getDefault();
		shlMavenSettingsEditor = new Shell(display);
		shlMavenSettingsEditor.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR);
		shlMavenSettingsEditor.setMinimumSize(new Point(663, 505));
		shlMavenSettingsEditor.setSize(663, 505);
		shlMavenSettingsEditor.setText("Maven Settings Editor");
		// createContents(new Settings(), new Maven());
	}

	/**
	 * create content
	 * 
	 * @param settings
	 * @param maven
	 */
	private final void createContents(final Settings settings, final Maven maven) {
		btnSave = new Button(shlMavenSettingsEditor, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				try {
					SettingsFactory.save(settings, maven);
					newSettings = settings;
					shlMavenSettingsEditor.close();
				} catch (IOException ex) {
					MessageBox messageBox = new MessageBox(shlMavenSettingsEditor, SWT.ICON_ERROR);
					messageBox.setMessage("Failed save settings.xml for Maven:" + maven.getHome().getPath() + ". Reason: " + ex.getMessage());
					messageBox.setText("Failed save settings.xml");
					messageBox.open();
				}
			}
		});
		btnSave.setText("Save");

		final TabFolder tabFolder = new TabFolder(shlMavenSettingsEditor, SWT.NONE);
		tabFolder.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				TabItem item = tabFolder.getItem(tabFolder.getSelectionIndex());
				if (item.getText().equals("Servers")) {
					GuiUtils.fillTableItem(tableServers, settings.getServers());
				} else if (item.getText().equals("Mirrors")) {
					GuiUtils.fillTableMirrorsItem(tableMirrors, settings.getMirrors());
				} else if (item.getText().equals("Proxies")) {
					GuiUtils.fillTableProxiesItem(tableProxies, settings.getProxies());
				} else if (item.getText().equals("Profiles")) {
					GuiUtils.fillListProfileItem(listProfiles, settings.getProfiles());
				} else if (item.getText().equals("Plugin Groups")) {
					GuiUtils.fillListItem(listPluginGroups, settings.getPluginGroups());
				} else if (item.getText().equals("Active Profiles")) {
					GuiUtils.fillListItem(listActiveProfiles, settings.getActiveProfiles());
				}

			}
		});

		Button btnCancel = new Button(shlMavenSettingsEditor, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				shlMavenSettingsEditor.close();
			}
		});
		btnCancel.setText("Cancel");
		GroupLayout gl_shlMavenSettingsEditor = new GroupLayout(shlMavenSettingsEditor);
		gl_shlMavenSettingsEditor.setHorizontalGroup(gl_shlMavenSettingsEditor.createParallelGroup(GroupLayout.TRAILING)
				.add(gl_shlMavenSettingsEditor.createSequentialGroup().addContainerGap(571, Short.MAX_VALUE).add(btnSave, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(btnCancel).addContainerGap())
				.add(tabFolder, GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE));
		gl_shlMavenSettingsEditor.setVerticalGroup(gl_shlMavenSettingsEditor.createParallelGroup(GroupLayout.TRAILING).add(
				gl_shlMavenSettingsEditor.createSequentialGroup().add(tabFolder, GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE).addPreferredGap(LayoutStyle.RELATED).add(gl_shlMavenSettingsEditor.createParallelGroup(GroupLayout.BASELINE).add(btnSave).add(btnCancel))));

		TabItem tbtmSettings = new TabItem(tabFolder, SWT.NONE);
		tbtmSettings.setText("Settings");

		Composite compositeSettigns = new Composite(tabFolder, SWT.NONE);
		compositeSettigns.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		tbtmSettings.setControl(compositeSettigns);

		Label lblLocalRepository = new Label(compositeSettigns, SWT.NONE);
		lblLocalRepository.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblLocalRepository.setText("Local Repository");

		textRepositoryPath = new Text(compositeSettigns, SWT.BORDER);
		textRepositoryPath.setText(GuiUtils.formateNull(settings.getLocalRepository()));
		textRepositoryPath.addKeyListener(new KeyAdapter() {
			@Override
			public final void keyReleased(KeyEvent e) {
				settings.setLocalRepository(textRepositoryPath.getText());
				validateLocalRepository(settings);
			}
		});

		Button buttonSelectRepoPath = new Button(compositeSettigns, SWT.NONE);
		buttonSelectRepoPath.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				DirectoryDialog directoryDialog = new DirectoryDialog(shlMavenSettingsEditor);
				directoryDialog.setText("Select Maven local repository");
				directoryDialog.setMessage("Select Maven local repository");

				String dir = directoryDialog.open();
				if (dir != null) {
					textRepositoryPath.setText(dir);
					settings.setLocalRepository(dir);
				} else {
					if (settings.getLocalRepository() == null) {
						settings.setLocalRepository("");
					}
				}
				validateLocalRepository(settings);
			}
		});
		buttonSelectRepoPath.setText("...");

		Button btnInteractiveMode = new Button(compositeSettigns, SWT.CHECK);
		btnInteractiveMode.setSelection(settings.isInteractiveMode());
		btnInteractiveMode.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.setInteractiveMode(((Button) e.getSource()).getSelection());
			}
		});
		btnInteractiveMode.setText("Interactive Mode");

		Button btnUsePluginRegistry = new Button(compositeSettigns, SWT.CHECK);
		btnUsePluginRegistry.setSelection(settings.isUsePluginRegistry());
		btnUsePluginRegistry.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.setUsePluginRegistry(((Button) e.getSource()).getSelection());
			}
		});
		btnUsePluginRegistry.setText("Use Plugin Registry");

		Button btnOffline = new Button(compositeSettigns, SWT.CHECK);
		btnOffline.setSelection(settings.isOffline());
		btnOffline.addSelectionListener(new SelectionAdapter() {
			public final void widgetSelected(SelectionEvent e) {
				settings.setOffline(((Button) e.getSource()).getSelection());
			}
		});
		btnOffline.setText("Offline");

		labelWarning = new Label(compositeSettigns, SWT.NONE);
		labelWarning.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labelWarning.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD | SWT.ITALIC));

		Label label_2 = new Label(compositeSettigns, SWT.SEPARATOR | SWT.HORIZONTAL);

		Label labelHelpLocalRepository = new Label(compositeSettigns, SWT.NONE);
		labelHelpLocalRepository.setToolTipText("The local repository.");
		labelHelpLocalRepository.setImage(ImageFactory.HELP);

		Label labelHelpInteractiveMode = new Label(compositeSettigns, SWT.NONE);
		labelHelpInteractiveMode.setToolTipText("Whether Maven should attempt to interact with the user for input.");
		labelHelpInteractiveMode.setImage(ImageFactory.HELP);

		labelHelpUsePluginRegistry = new Label(compositeSettigns, SWT.NONE);
		labelHelpUsePluginRegistry.setToolTipText("Whether Maven should use the plugin-registry.xml file to manage plugin versions.");
		labelHelpUsePluginRegistry.setImage(ImageFactory.HELP);

		Label labelHelpOffline = new Label(compositeSettigns, SWT.NONE);
		labelHelpOffline.setToolTipText("Indicate whether maven should operate in offline mode full-time.");
		labelHelpOffline.setImage(ImageFactory.HELP);
		GroupLayout gl_compositeSettigns = new GroupLayout(compositeSettigns);
		gl_compositeSettigns.setHorizontalGroup(gl_compositeSettigns
				.createParallelGroup(GroupLayout.LEADING)
				.add(gl_compositeSettigns.createSequentialGroup().addContainerGap().add(lblLocalRepository).add(30).add(textRepositoryPath, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE).addPreferredGap(LayoutStyle.RELATED).add(buttonSelectRepoPath).add(12)
						.add(labelHelpLocalRepository).add(10)).add(gl_compositeSettigns.createSequentialGroup().add(10).add(labelWarning, GroupLayout.PREFERRED_SIZE, 584, GroupLayout.PREFERRED_SIZE))
				.add(gl_compositeSettigns.createSequentialGroup().add(12).add(label_2, GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE).add(12))
				.add(gl_compositeSettigns.createSequentialGroup().addContainerGap().add(btnInteractiveMode).addPreferredGap(LayoutStyle.RELATED, 492, Short.MAX_VALUE).add(labelHelpInteractiveMode).addContainerGap())
				.add(gl_compositeSettigns.createSequentialGroup().addContainerGap().add(btnUsePluginRegistry).addPreferredGap(LayoutStyle.RELATED, 478, Short.MAX_VALUE).add(labelHelpUsePluginRegistry).addContainerGap())
				.add(gl_compositeSettigns.createSequentialGroup().addContainerGap().add(btnOffline).addPreferredGap(LayoutStyle.RELATED, 545, Short.MAX_VALUE).add(labelHelpOffline).addContainerGap()));
		gl_compositeSettigns.setVerticalGroup(gl_compositeSettigns.createParallelGroup(GroupLayout.LEADING).add(
				gl_compositeSettigns
						.createSequentialGroup()
						.add(gl_compositeSettigns
								.createParallelGroup(GroupLayout.LEADING)
								.add(gl_compositeSettigns.createSequentialGroup().add(18)
										.add(gl_compositeSettigns.createParallelGroup(GroupLayout.BASELINE).add(textRepositoryPath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).add(lblLocalRepository).add(labelHelpLocalRepository)))
								.add(gl_compositeSettigns.createSequentialGroup().addContainerGap().add(buttonSelectRepoPath))).add(12).add(labelWarning).add(6).add(label_2).add(18)
						.add(gl_compositeSettigns.createParallelGroup(GroupLayout.BASELINE).add(btnInteractiveMode).add(labelHelpInteractiveMode)).add(18)
						.add(gl_compositeSettigns.createParallelGroup(GroupLayout.BASELINE).add(btnUsePluginRegistry).add(labelHelpUsePluginRegistry)).add(18).add(gl_compositeSettigns.createParallelGroup(GroupLayout.BASELINE).add(btnOffline).add(labelHelpOffline)).add(233)));
		compositeSettigns.setLayout(gl_compositeSettigns);

		TabItem tbtmActiveProfiles = new TabItem(tabFolder, SWT.NONE);
		tbtmActiveProfiles.setText("Active Profiles");

		Composite compositeActiveProfiles = new Composite(tabFolder, SWT.NONE);
		tbtmActiveProfiles.setControl(compositeActiveProfiles);

		listActiveProfiles = new List(compositeActiveProfiles, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);

		Button btnActiveProfilesDeleteAll = new Button(compositeActiveProfiles, SWT.NONE);
		btnActiveProfilesDeleteAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getActiveProfiles().clear();
				listActiveProfiles.removeAll();
			}
		});
		btnActiveProfilesDeleteAll.setText("Delete All");

		final Button btnActiveProfilesDelete = new Button(compositeActiveProfiles, SWT.NONE);
		btnActiveProfilesDelete.setEnabled(false);
		btnActiveProfilesDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getActiveProfiles().remove(listActiveProfiles.getSelectionIndex());
				listActiveProfiles.remove(listActiveProfiles.getSelectionIndex());
			}
		});
		btnActiveProfilesDelete.setText("Delete");

		final Button btnActiveProfilesReplace = new Button(compositeActiveProfiles, SWT.NONE);
		btnActiveProfilesReplace.setEnabled(false);
		btnActiveProfilesReplace.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmReplaceActiveProfiles(shlMavenSettingsEditor, settings, listActiveProfiles.getSelectionIndex());
			}
		});
		btnActiveProfilesReplace.setText("Replace");

		Button btnActiveProfilesAdd = new Button(compositeActiveProfiles, SWT.NONE);
		btnActiveProfilesAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				FrmSelectProfile frmSelectProfile = new FrmSelectProfile(shlMavenSettingsEditor, settings.getProfiles(), settings.getActiveProfiles());
				String activeProfile = frmSelectProfile.open();
				if (activeProfile != null) {
					settings.addActiveProfile(activeProfile);
					GuiUtils.fillListItem(listActiveProfiles, settings.getActiveProfiles());
				}
			}
		});
		btnActiveProfilesAdd.setText("Add");

		listActiveProfiles.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = listActiveProfiles.getSelectionIndex();
				if (index != -1) {
					openFrmReplaceActiveProfiles(shlMavenSettingsEditor, settings, index);
					btnActiveProfilesReplace.setEnabled(true);
					btnActiveProfilesDelete.setEnabled(true);
				} else {
					btnActiveProfilesReplace.setEnabled(false);
					btnActiveProfilesDelete.setEnabled(false);
				}
			}
		});
		listActiveProfiles.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = listActiveProfiles.getSelectionIndex();
				btnActiveProfilesReplace.setEnabled(index != -1);
				btnActiveProfilesDelete.setEnabled(index != -1);
			}
		});
		GroupLayout gl_compositeActiveProfiles = new GroupLayout(compositeActiveProfiles);
		gl_compositeActiveProfiles.setHorizontalGroup(gl_compositeActiveProfiles
				.createParallelGroup(GroupLayout.TRAILING)
				.add(gl_compositeActiveProfiles.createSequentialGroup().addContainerGap(323, Short.MAX_VALUE).add(btnActiveProfilesAdd).addPreferredGap(LayoutStyle.RELATED).add(btnActiveProfilesReplace).addPreferredGap(LayoutStyle.RELATED).add(btnActiveProfilesDelete)
						.addPreferredGap(LayoutStyle.RELATED).add(btnActiveProfilesDeleteAll)).add(GroupLayout.LEADING, listActiveProfiles, GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE));
		gl_compositeActiveProfiles.setVerticalGroup(gl_compositeActiveProfiles.createParallelGroup(GroupLayout.LEADING).add(
				GroupLayout.TRAILING,
				gl_compositeActiveProfiles.createSequentialGroup().add(listActiveProfiles, GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_compositeActiveProfiles.createParallelGroup(GroupLayout.BASELINE).add(btnActiveProfilesDeleteAll).add(btnActiveProfilesDelete).add(btnActiveProfilesReplace).add(btnActiveProfilesAdd))));
		compositeActiveProfiles.setLayout(gl_compositeActiveProfiles);

		TabItem tbtmPluginGroups = new TabItem(tabFolder, SWT.NONE);
		tbtmPluginGroups.setText("Plugin Groups");

		Composite compositePluginGroups = new Composite(tabFolder, SWT.NONE);
		tbtmPluginGroups.setControl(compositePluginGroups);

		listPluginGroups = new List(compositePluginGroups, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		final Button btnPluginGroupsDelete = new Button(compositePluginGroups, SWT.NONE);
		btnPluginGroupsDelete.setEnabled(false);
		final Button btnPluginGroupsReplace = new Button(compositePluginGroups, SWT.NONE);
		btnPluginGroupsReplace.setEnabled(false);

		listPluginGroups.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = listPluginGroups.getSelectionIndex();
				if (index != -1) {
					openFrmLoadingPluginGroups(shlMavenSettingsEditor, settings, index, true);
					btnPluginGroupsDelete.setEnabled(true);
					btnPluginGroupsReplace.setEnabled(true);
				} else {
					btnPluginGroupsDelete.setEnabled(false);
					btnPluginGroupsReplace.setEnabled(false);
				}
			}
		});
		listPluginGroups.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = listPluginGroups.getSelectionIndex();
				btnPluginGroupsDelete.setEnabled(index != -1);
				btnPluginGroupsReplace.setEnabled(index != -1);
			}
		});

		Button btnPluginGroupsDeleteAll = new Button(compositePluginGroups, SWT.NONE);
		btnPluginGroupsDeleteAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getPluginGroups().clear();
				listPluginGroups.removeAll();
			}
		});
		btnPluginGroupsDeleteAll.setText("Delete All");

		btnPluginGroupsDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getPluginGroups().remove(listPluginGroups.getSelectionIndex());
				listPluginGroups.remove(listPluginGroups.getSelectionIndex());
			}
		});
		btnPluginGroupsDelete.setText("Delete");

		btnPluginGroupsReplace.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmLoadingPluginGroups(shlMavenSettingsEditor, settings, listPluginGroups.getSelectionIndex(), true);
			}
		});
		btnPluginGroupsReplace.setText("Replace");

		Button btnPluginGroupsAdd = new Button(compositePluginGroups, SWT.NONE);
		btnPluginGroupsAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmLoadingPluginGroups(shlMavenSettingsEditor, settings, listPluginGroups.getSelectionIndex(), false);
			}
		});
		btnPluginGroupsAdd.setText("Add");
		GroupLayout gl_compositePluginGroups = new GroupLayout(compositePluginGroups);
		gl_compositePluginGroups.setHorizontalGroup(gl_compositePluginGroups
				.createParallelGroup(GroupLayout.LEADING)
				.add(GroupLayout.TRAILING,
						gl_compositePluginGroups.createSequentialGroup().add(323).add(btnPluginGroupsAdd).addPreferredGap(LayoutStyle.RELATED).add(btnPluginGroupsReplace).addPreferredGap(LayoutStyle.RELATED).add(btnPluginGroupsDelete).addPreferredGap(LayoutStyle.RELATED)
								.add(btnPluginGroupsDeleteAll)).add(listPluginGroups, GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE));
		gl_compositePluginGroups.setVerticalGroup(gl_compositePluginGroups.createParallelGroup(GroupLayout.LEADING).add(
				GroupLayout.TRAILING,
				gl_compositePluginGroups.createSequentialGroup().add(listPluginGroups, GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_compositePluginGroups.createParallelGroup(GroupLayout.BASELINE).add(btnPluginGroupsDeleteAll).add(btnPluginGroupsDelete).add(btnPluginGroupsReplace).add(btnPluginGroupsAdd))));
		compositePluginGroups.setLayout(gl_compositePluginGroups);

		TabItem tbtmServers = new TabItem(tabFolder, SWT.NONE);
		tbtmServers.setText("Servers");

		Composite compositeServers = new Composite(tabFolder, SWT.NONE);
		tbtmServers.setControl(compositeServers);

		final Button btnServersDelete = new Button(compositeServers, SWT.NONE);
		btnServersDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = tableServers.getSelectionIndex();
				settings.getServers().remove(index);
				tableServers.remove(index);
			}
		});
		btnServersDelete.setEnabled(false);
		btnServersDelete.setText("Delete");

		final Button btnServersEdit = new Button(compositeServers, SWT.NONE);
		btnServersEdit.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmServersForEditing(tableServers.getSelectionIndex(), shlMavenSettingsEditor, settings);
			}
		});
		btnServersEdit.setText("Edit");
		btnServersEdit.setEnabled(false);

		tableServers = new Table(compositeServers, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		tableServers.addMouseListener(new MouseAdapter() {
			@Override
			public final void mouseDoubleClick(MouseEvent e) {
				int index = tableServers.getSelectionIndex();
				if (index != -1) {
					openFrmServersForEditing(index, shlMavenSettingsEditor, settings);
					btnServersEdit.setEnabled(true);
					btnServersDelete.setEnabled(true);
				} else {
					btnServersEdit.setEnabled(false);
					btnServersDelete.setEnabled(false);
				}
			}
		});
		GuiUtils.fillTableItem(tableServers, settings.getServers());
		tableServers.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = tableServers.getSelectionIndex();
				btnServersDelete.setEnabled(index != -1);
				btnServersEdit.setEnabled(index != -1);
			}
		});
		tableServers.setHeaderVisible(true);
		tableServers.setLinesVisible(true);

		Button btnServersAdd = new Button(compositeServers, SWT.NONE);
		btnServersAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				FrmServer frmServers = new FrmServer(shlMavenSettingsEditor, settings.getServers());
				Server server = frmServers.open(null);
				if (server != null) {
					settings.addServer(server);
					GuiUtils.fillTableItem(tableServers, settings.getServers());
				}

			}
		});
		btnServersAdd.setText("Add");

		Button btnServersDeleteAll = new Button(compositeServers, SWT.NONE);
		btnServersDeleteAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getServers().clear();
				GuiUtils.fillTableItem(tableServers, settings.getServers());
			}
		});
		btnServersDeleteAll.setText("Delete All");
		GroupLayout gl_compositeServers = new GroupLayout(compositeServers);
		gl_compositeServers.setHorizontalGroup(gl_compositeServers
				.createParallelGroup(GroupLayout.TRAILING)
				.add(gl_compositeServers.createSequentialGroup().addContainerGap(368, Short.MAX_VALUE).add(btnServersAdd).addPreferredGap(LayoutStyle.RELATED).add(btnServersEdit).addPreferredGap(LayoutStyle.RELATED).add(btnServersDelete).addPreferredGap(LayoutStyle.RELATED)
						.add(btnServersDeleteAll)).add(tableServers, GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE));
		gl_compositeServers.setVerticalGroup(gl_compositeServers.createParallelGroup(GroupLayout.LEADING).add(
				GroupLayout.TRAILING,
				gl_compositeServers.createSequentialGroup().add(tableServers, GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_compositeServers.createParallelGroup(GroupLayout.BASELINE).add(btnServersDeleteAll).add(btnServersDelete).add(btnServersEdit).add(btnServersAdd))));

		TableColumn tblclmnServersID = new TableColumn(tableServers, SWT.CENTER);
		tblclmnServersID.setResizable(true);
		tblclmnServersID.setMoveable(true);
		tblclmnServersID.setWidth(213);
		tblclmnServersID.setText("ID");

		TableColumn tblclmnServersName = new TableColumn(tableServers, SWT.CENTER);
		tblclmnServersName.setResizable(true);
		tblclmnServersName.setMoveable(true);
		tblclmnServersName.setWidth(213);
		tblclmnServersName.setText("Username");

		TableColumn tblclmnServersPassword = new TableColumn(tableServers, SWT.CENTER);
		tblclmnServersPassword.setMoveable(true);
		tblclmnServersPassword.setWidth(213);
		tblclmnServersPassword.setResizable(true);
		tblclmnServersPassword.setText("Password");
		compositeServers.setLayout(gl_compositeServers);

		TabItem tbtmMirrors = new TabItem(tabFolder, SWT.NONE);
		tbtmMirrors.setText("Mirrors");

		Composite compositeMirrors = new Composite(tabFolder, SWT.NONE);
		tbtmMirrors.setControl(compositeMirrors);

		final Button btnMirrorsDelete = new Button(compositeMirrors, SWT.NONE);
		btnMirrorsDelete.setEnabled(false);

		final Button btnMirrorsEdit = new Button(compositeMirrors, SWT.NONE);
		btnMirrorsEdit.setEnabled(false);

		tableMirrors = new Table(compositeMirrors, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
		GuiUtils.fillTableMirrorsItem(tableMirrors, settings.getMirrors());
		tableMirrors.addMouseListener(new MouseAdapter() {
			@Override
			public final void mouseDoubleClick(MouseEvent e) {
				int index = tableMirrors.getSelectionIndex();
				if (index != -1) {
					openFrmMirrorsForEditing(shlMavenSettingsEditor, settings, maven, index);
					btnMirrorsDelete.setEnabled(true);
					btnMirrorsEdit.setEnabled(true);
				} else {
					btnMirrorsDelete.setEnabled(false);
					btnMirrorsEdit.setEnabled(false);
				}
			}
		});
		tableMirrors.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				btnMirrorsDelete.setEnabled(tableMirrors.getSelectionIndex() != -1);
				btnMirrorsEdit.setEnabled(tableMirrors.getSelectionIndex() != -1);
			}
		});
		tableMirrors.setHeaderVisible(true);
		tableMirrors.setLinesVisible(true);

		btnMirrorsDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getMirrors().remove(tableMirrors.getSelectionIndex());
				tableMirrors.remove(tableMirrors.getSelectionIndex());
			}
		});
		btnMirrorsDelete.setText("Delete");

		btnMirrorsEdit.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmMirrorsForEditing(shlMavenSettingsEditor, settings, maven, tableMirrors.getSelectionIndex());
			}
		});
		btnMirrorsEdit.setText("Edit");

		Button btnMirrorsAdd = new Button(compositeMirrors, SWT.NONE);
		btnMirrorsAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				FrmMirrors frmMirrors = new FrmMirrors(shlMavenSettingsEditor, settings.getMirrors());
				Mirror mirror = frmMirrors.open(null, maven.getVersion());
				if (mirror != null) {
					settings.addMirror(mirror);
					GuiUtils.fillTableMirrorsItem(tableMirrors, settings.getMirrors());
				}

			}
		});
		btnMirrorsAdd.setText("Add");

		Button btnMirrorsDeleteAll = new Button(compositeMirrors, SWT.NONE);
		btnMirrorsDeleteAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getMirrors().clear();
				GuiUtils.fillTableMirrorsItem(tableMirrors, settings.getMirrors());
			}
		});
		btnMirrorsDeleteAll.setText("Delete All");
		GroupLayout gl_compositeMirrors = new GroupLayout(compositeMirrors);
		gl_compositeMirrors.setHorizontalGroup(gl_compositeMirrors
				.createParallelGroup(GroupLayout.TRAILING)
				.add(gl_compositeMirrors.createSequentialGroup().addContainerGap(368, Short.MAX_VALUE).add(btnMirrorsAdd).addPreferredGap(LayoutStyle.RELATED).add(btnMirrorsEdit).addPreferredGap(LayoutStyle.RELATED).add(btnMirrorsDelete).addPreferredGap(LayoutStyle.RELATED)
						.add(btnMirrorsDeleteAll)).add(GroupLayout.LEADING, tableMirrors, GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE));
		gl_compositeMirrors.setVerticalGroup(gl_compositeMirrors.createParallelGroup(GroupLayout.LEADING).add(
				GroupLayout.TRAILING,
				gl_compositeMirrors.createSequentialGroup().add(tableMirrors, GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_compositeMirrors.createParallelGroup(GroupLayout.BASELINE).add(btnMirrorsDeleteAll).add(btnMirrorsDelete).add(btnMirrorsEdit).add(btnMirrorsAdd))));

		TableColumn tblclmnMirrorsId = new TableColumn(tableMirrors, SWT.CENTER);
		tblclmnMirrorsId.setMoveable(true);
		tblclmnMirrorsId.setWidth(160);
		tblclmnMirrorsId.setText("ID");

		TableColumn tblclmnName = new TableColumn(tableMirrors, SWT.CENTER);
		tblclmnName.setMoveable(true);
		tblclmnName.setWidth(160);
		tblclmnName.setText("Name");

		TableColumn tblclmnMirrorsURL = new TableColumn(tableMirrors, SWT.CENTER);
		tblclmnMirrorsURL.setMoveable(true);
		tblclmnMirrorsURL.setWidth(160);
		tblclmnMirrorsURL.setText("URL");

		TableColumn tblclmnMirrorsMirrorof = new TableColumn(tableMirrors, SWT.CENTER);
		tblclmnMirrorsMirrorof.setMoveable(true);
		tblclmnMirrorsMirrorof.setWidth(160);
		tblclmnMirrorsMirrorof.setText("MirrorOf");
		compositeMirrors.setLayout(gl_compositeMirrors);

		TabItem tbtmProxies = new TabItem(tabFolder, SWT.NONE);
		tbtmProxies.setText("Proxies");

		Composite compositeProxies = new Composite(tabFolder, SWT.NONE);
		tbtmProxies.setControl(compositeProxies);

		final Button btnProxiesDelete = new Button(compositeProxies, SWT.NONE);
		btnProxiesDelete.setEnabled(false);
		btnProxiesDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getProxies().remove(tableProxies.getSelectionIndex());
				GuiUtils.fillTableProxiesItem(tableProxies, settings.getProxies());
			}
		});
		btnProxiesDelete.setText("Delete");

		final Button btnProxiesEdit = new Button(compositeProxies, SWT.NONE);
		btnProxiesEdit.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmProxiesForEditing(shlMavenSettingsEditor, settings, tableProxies.getSelectionIndex());
			}
		});
		btnProxiesEdit.setText("Edit");
		btnProxiesEdit.setEnabled(false);
		Button btnProxysieAdd = new Button(compositeProxies, SWT.NONE);
		btnProxysieAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				FrmProxy frmProxies = new FrmProxy(shlMavenSettingsEditor, settings.getProxies());
				Proxy proxy = frmProxies.open(null);
				if (proxy != null) {
					settings.addProxy(proxy);
					GuiUtils.fillTableProxiesItem(tableProxies, settings.getProxies());
				}
			}
		});
		btnProxysieAdd.setText("Add");

		tableProxies = new Table(compositeProxies, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		GuiUtils.fillTableProxiesItem(tableProxies, settings.getProxies());
		tableProxies.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = tableProxies.getSelectionIndex();
				if (index != -1) {
					openFrmProxiesForEditing(shlMavenSettingsEditor, settings, index);
					btnProxiesEdit.setEnabled(true);
					btnProxiesDelete.setEnabled(true);
				} else {
					btnProxiesEdit.setEnabled(false);
					btnProxiesDelete.setEnabled(false);
				}

			}
		});
		tableProxies.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				btnProxiesEdit.setEnabled(tableProxies.getSelectionIndex() != -1);
				btnProxiesDelete.setEnabled(tableProxies.getSelectionIndex() != -1);
			}
		});
		tableProxies.setHeaderVisible(true);
		tableProxies.setLinesVisible(true);

		Button btnProxiesDeleteAll = new Button(compositeProxies, SWT.NONE);
		btnProxiesDeleteAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getProxies().clear();
				GuiUtils.fillTableProxiesItem(tableProxies, settings.getProxies());
			}
		});
		btnProxiesDeleteAll.setText("Delete All");
		GroupLayout gl_compositeProxies = new GroupLayout(compositeProxies);
		gl_compositeProxies.setHorizontalGroup(gl_compositeProxies
				.createParallelGroup(GroupLayout.TRAILING)
				.add(gl_compositeProxies.createSequentialGroup().addContainerGap(368, Short.MAX_VALUE).add(btnProxysieAdd).addPreferredGap(LayoutStyle.RELATED).add(btnProxiesEdit).addPreferredGap(LayoutStyle.RELATED).add(btnProxiesDelete).addPreferredGap(LayoutStyle.RELATED)
						.add(btnProxiesDeleteAll)).add(GroupLayout.LEADING, tableProxies, GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE));
		gl_compositeProxies.setVerticalGroup(gl_compositeProxies.createParallelGroup(GroupLayout.TRAILING).add(
				gl_compositeProxies.createSequentialGroup().add(tableProxies, GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_compositeProxies.createParallelGroup(GroupLayout.BASELINE).add(btnProxiesDeleteAll).add(btnProxiesDelete).add(btnProxiesEdit).add(btnProxysieAdd))));

		TableColumn tblclmnProxiesId = new TableColumn(tableProxies, SWT.CENTER);
		tblclmnProxiesId.setMoveable(true);
		tblclmnProxiesId.setWidth(100);
		tblclmnProxiesId.setText("ID");

		TableColumn tblclmnProxiesUsername = new TableColumn(tableProxies, SWT.CENTER);
		tblclmnProxiesUsername.setMoveable(true);
		tblclmnProxiesUsername.setWidth(100);
		tblclmnProxiesUsername.setText("Username");

		TableColumn tblclmnProxiesPassword = new TableColumn(tableProxies, SWT.CENTER);
		tblclmnProxiesPassword.setMoveable(true);
		tblclmnProxiesPassword.setWidth(100);
		tblclmnProxiesPassword.setText("Password");

		TableColumn tblclmnProxiesProtocol = new TableColumn(tableProxies, SWT.CENTER);
		tblclmnProxiesProtocol.setMoveable(true);
		tblclmnProxiesProtocol.setWidth(91);
		tblclmnProxiesProtocol.setText("Protocol");

		TableColumn tblclmnProxiesHost = new TableColumn(tableProxies, SWT.CENTER);
		tblclmnProxiesHost.setMoveable(true);
		tblclmnProxiesHost.setWidth(100);
		tblclmnProxiesHost.setText("Host");

		TableColumn tblclmnPortPort = new TableColumn(tableProxies, SWT.CENTER);
		tblclmnPortPort.setMoveable(true);
		tblclmnPortPort.setWidth(100);
		tblclmnPortPort.setText("Port");

		TableColumn tblclmnProxiesActive = new TableColumn(tableProxies, SWT.CENTER | SWT.CHECK);
		tblclmnProxiesActive.setResizable(false);
		tblclmnProxiesActive.setWidth(100);
		tblclmnProxiesActive.setText("Active");
		compositeProxies.setLayout(gl_compositeProxies);

		TabItem tbtmProfiles = new TabItem(tabFolder, SWT.NONE);
		tbtmProfiles.setText("Profiles");

		Composite compositeProfiles = new Composite(tabFolder, SWT.NONE);
		tbtmProfiles.setControl(compositeProfiles);

		Button btnProfileDeleteAll = new Button(compositeProfiles, SWT.NONE);
		btnProfileDeleteAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				settings.getProfiles().clear();
				settings.getActiveProfiles().clear();
				GuiUtils.fillListProfileItem(listProfiles, settings.getProfiles());
			}
		});
		btnProfileDeleteAll.setText("Delete all");

		final Button btnProfileDelete = new Button(compositeProfiles, SWT.NONE);
		btnProfileDelete.setEnabled(false);
		btnProfileDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = listProfiles.getSelectionIndex();
				String profileId = settings.getProfiles().get(index).getId();
				settings.getProfiles().remove(index);
				GuiUtils.fillListProfileItem(listProfiles, settings.getProfiles());
				removeProfileFromActive(settings, profileId);

			}
		});

		btnProfileDelete.setText("Delete");

		final Button btnProfileEdit = new Button(compositeProfiles, SWT.NONE);
		btnProfileEdit.setEnabled(false);
		btnProfileEdit.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				openFrmProfileForEdit(shlMavenSettingsEditor, settings, listProfiles.getSelectionIndex());
			}
		});
		btnProfileEdit.setText("Edit");

		Button btnProfileAdd = new Button(compositeProfiles, SWT.NONE);
		btnProfileAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				FrmProfile frmProfile = new FrmProfile(shlMavenSettingsEditor, settings.getProfiles());
				Profile profile = frmProfile.open(null);
				if (profile != null && GuiUtils.formateNull(profile.getId()).trim().length() != 0) {
					settings.addProfile(profile);
					GuiUtils.fillListProfileItem(listProfiles, settings.getProfiles());
				}
			}
		});
		btnProfileAdd.setText("Add");

		Group group = new Group(compositeProfiles, SWT.NONE);

		final TabFolder tabFolderProfilesRoot = new TabFolder(compositeProfiles, SWT.NONE);
		// tabFolderProfilesRoot.addSelectionListener(new SelectionAdapter() {
		// public final void widgetSelected(SelectionEvent e) {
		// TabItem tabItem =
		// tabFolderProfilesRoot.getItem(tabFolderProfilesRoot.getSelectionIndex());
		//
		// }
		// });
		GroupLayout gl_compositeProfiles = new GroupLayout(compositeProfiles);
		gl_compositeProfiles.setHorizontalGroup(gl_compositeProfiles
				.createParallelGroup(GroupLayout.TRAILING)
				.add(gl_compositeProfiles.createSequentialGroup().addContainerGap(377, Short.MAX_VALUE).add(btnProfileAdd).addPreferredGap(LayoutStyle.RELATED).add(btnProfileEdit).addPreferredGap(LayoutStyle.RELATED).add(btnProfileDelete).addPreferredGap(LayoutStyle.RELATED)
						.add(btnProfileDeleteAll).addContainerGap()).add(GroupLayout.LEADING, group, GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE).add(GroupLayout.LEADING, tabFolderProfilesRoot, GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE));
		gl_compositeProfiles.setVerticalGroup(gl_compositeProfiles.createParallelGroup(GroupLayout.LEADING).add(
				gl_compositeProfiles.createSequentialGroup().add(group, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_compositeProfiles.createParallelGroup(GroupLayout.BASELINE).add(btnProfileDeleteAll).add(btnProfileDelete).add(btnProfileEdit).add(btnProfileAdd)).addPreferredGap(LayoutStyle.RELATED)
						.add(tabFolderProfilesRoot, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)));

		TabItem tbtmActivation = new TabItem(tabFolderProfilesRoot, SWT.NONE);
		tbtmActivation.setText("Activation");

		Composite compositeActivation = new Composite(tabFolderProfilesRoot, SWT.NONE);
		tbtmActivation.setControl(compositeActivation);

		final Button btnActiveByDefault = new Button(compositeActivation, SWT.CHECK);
		btnActiveByDefault.setEnabled(false);
		btnActiveByDefault.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		btnActiveByDefault.setText("Active By Default");

		Label lblJdk = new Label(compositeActivation, SWT.NONE);
		lblJdk.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblJdk.setText("JDK");

		textProfileJDK = new Text(compositeActivation, SWT.BORDER);

		Label label = new Label(compositeActivation, SWT.SEPARATOR | SWT.HORIZONTAL);

		Group grpOs = new Group(compositeActivation, SWT.NONE);
		grpOs.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		grpOs.setText("OS");
		GroupLayout gl_compositeActivation = new GroupLayout(compositeActivation);
		gl_compositeActivation.setHorizontalGroup(gl_compositeActivation.createParallelGroup(GroupLayout.LEADING).add(
				gl_compositeActivation
						.createSequentialGroup()
						.addContainerGap()
						.add(gl_compositeActivation.createParallelGroup(GroupLayout.LEADING).add(grpOs, GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE).add(label, GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE).add(btnActiveByDefault)
								.add(GroupLayout.TRAILING, gl_compositeActivation.createSequentialGroup().add(lblJdk).add(18).add(textProfileJDK, GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE))).addContainerGap()));
		gl_compositeActivation.setVerticalGroup(gl_compositeActivation.createParallelGroup(GroupLayout.LEADING).add(
				gl_compositeActivation.createSequentialGroup().addContainerGap().add(btnActiveByDefault).addPreferredGap(LayoutStyle.UNRELATED).add(label, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_compositeActivation.createParallelGroup(GroupLayout.BASELINE).add(textProfileJDK, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).add(lblJdk)).addPreferredGap(LayoutStyle.RELATED)
						.add(grpOs, GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)));

		Label lblOsName = new Label(grpOs, SWT.NONE);
		lblOsName.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblOsName.setText("Name");

		textOsName = new Text(grpOs, SWT.BORDER);
		textOsName.setEditable(false);

		Label lblOsFamily = new Label(grpOs, SWT.NONE);
		lblOsFamily.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblOsFamily.setText("Family");

		textOsFamily = new Text(grpOs, SWT.BORDER);
		textOsFamily.setEditable(false);

		Label lblOsArch = new Label(grpOs, SWT.NONE);
		lblOsArch.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblOsArch.setText("Arch");

		textOsArch = new Text(grpOs, SWT.BORDER);
		textOsArch.setEditable(false);

		Label lblVersion = new Label(grpOs, SWT.NONE);
		lblVersion.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblVersion.setText("Version");

		textOsVersion = new Text(grpOs, SWT.BORDER);
		textOsVersion.setEditable(false);
		GroupLayout gl_grpOs = new GroupLayout(grpOs);
		gl_grpOs.setHorizontalGroup(gl_grpOs.createParallelGroup(GroupLayout.LEADING).add(
				gl_grpOs.createSequentialGroup()
						.addContainerGap()
						.add(gl_grpOs.createParallelGroup(GroupLayout.LEADING, false).add(lblVersion).add(lblOsArch, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE).add(lblOsName, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
								.add(lblOsFamily, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(LayoutStyle.RELATED)
						.add(gl_grpOs.createParallelGroup(GroupLayout.LEADING).add(textOsName, GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE).add(GroupLayout.TRAILING, textOsArch, GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
								.add(textOsVersion, GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE).add(GroupLayout.TRAILING, textOsFamily, GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)).addContainerGap()));
		gl_grpOs.setVerticalGroup(gl_grpOs.createParallelGroup(GroupLayout.LEADING).add(
				gl_grpOs.createSequentialGroup().addContainerGap().add(gl_grpOs.createParallelGroup(GroupLayout.BASELINE).add(lblOsName).add(textOsName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_grpOs.createParallelGroup(GroupLayout.BASELINE).add(lblOsFamily).add(textOsFamily, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_grpOs.createParallelGroup(GroupLayout.BASELINE).add(lblOsArch).add(textOsArch, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.RELATED)
						.add(gl_grpOs.createParallelGroup(GroupLayout.BASELINE).add(lblVersion).add(textOsVersion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		grpOs.setLayout(gl_grpOs);
		compositeActivation.setLayout(gl_compositeActivation);

		TabItem tbtmProperties = new TabItem(tabFolderProfilesRoot, SWT.NONE);
		tbtmProperties.setText("Properties");

		Composite compositeProfileProperties = new Composite(tabFolderProfilesRoot, SWT.NONE);
		tbtmProperties.setControl(compositeProfileProperties);

		tableProperties = new Table(compositeProfileProperties, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
		tableProperties.setHeaderVisible(true);
		tableProperties.setLinesVisible(true);
		GroupLayout gl_compositeProfileProperties = new GroupLayout(compositeProfileProperties);
		gl_compositeProfileProperties.setHorizontalGroup(gl_compositeProfileProperties.createParallelGroup(GroupLayout.LEADING).add(tableProperties, GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE));
		gl_compositeProfileProperties.setVerticalGroup(gl_compositeProfileProperties.createParallelGroup(GroupLayout.LEADING).add(tableProperties, GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE));

		TableColumn tblclmnPropertyName = new TableColumn(tableProperties, SWT.NONE);
		tblclmnPropertyName.setWidth(307);
		tblclmnPropertyName.setText("Name");

		TableColumn tblclmnPropertyValue = new TableColumn(tableProperties, SWT.NONE);
		tblclmnPropertyValue.setWidth(305);
		tblclmnPropertyValue.setText("Value");
		compositeProfileProperties.setLayout(gl_compositeProfileProperties);

		TabItem tbtmRepositories = new TabItem(tabFolderProfilesRoot, SWT.NONE);
		tbtmRepositories.setText("Repositories");

		Composite compositeRepositories = new Composite(tabFolderProfilesRoot, SWT.NONE);
		tbtmRepositories.setControl(compositeRepositories);

		tableRepositories = new Table(compositeRepositories, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
		tableRepositories.setHeaderVisible(true);
		tableRepositories.setLinesVisible(true);
		GroupLayout gl_compositeRepositories = new GroupLayout(compositeRepositories);
		gl_compositeRepositories.setHorizontalGroup(gl_compositeRepositories.createParallelGroup(GroupLayout.LEADING).add(tableRepositories, GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE));
		gl_compositeRepositories.setVerticalGroup(gl_compositeRepositories.createParallelGroup(GroupLayout.LEADING).add(tableRepositories, GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE));

		TableColumn tblclmnRepositoriesId = new TableColumn(tableRepositories, SWT.CENTER);
		tblclmnRepositoriesId.setMoveable(true);
		tblclmnRepositoriesId.setWidth(90);
		tblclmnRepositoriesId.setText("ID");

		TableColumn tblclmnRepositoriesName = new TableColumn(tableRepositories, SWT.CENTER);
		tblclmnRepositoriesName.setMoveable(true);
		tblclmnRepositoriesName.setWidth(142);
		tblclmnRepositoriesName.setText("Name");

		TableColumn tblclmnRepositoriesUrl = new TableColumn(tableRepositories, SWT.CENTER);
		tblclmnRepositoriesUrl.setMoveable(true);
		tblclmnRepositoriesUrl.setWidth(234);
		tblclmnRepositoriesUrl.setText("URL");

		TableColumn tblclmnRepositoriesLayout = new TableColumn(tableRepositories, SWT.CENTER);
		tblclmnRepositoriesLayout.setMoveable(true);
		tblclmnRepositoriesLayout.setWidth(116);
		tblclmnRepositoriesLayout.setText("Layout");
		compositeRepositories.setLayout(gl_compositeRepositories);

		TabItem tbtmPluginRepositories = new TabItem(tabFolderProfilesRoot, SWT.NONE);
		tbtmPluginRepositories.setText("Plugin Repositories");

		Composite compositePluginRepositories = new Composite(tabFolderProfilesRoot, SWT.NONE);
		tbtmPluginRepositories.setControl(compositePluginRepositories);

		tablePluginRepositories = new Table(compositePluginRepositories, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);

		tablePluginRepositories.setHeaderVisible(true);
		tablePluginRepositories.setLinesVisible(true);
		GroupLayout gl_compositePluginRepositories = new GroupLayout(compositePluginRepositories);
		gl_compositePluginRepositories.setHorizontalGroup(gl_compositePluginRepositories.createParallelGroup(GroupLayout.LEADING).add(tablePluginRepositories, GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE));
		gl_compositePluginRepositories.setVerticalGroup(gl_compositePluginRepositories.createParallelGroup(GroupLayout.LEADING).add(tablePluginRepositories, GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE));

		TableColumn tblclmnPluginRepositoriesId = new TableColumn(tablePluginRepositories, SWT.CENTER);
		tblclmnPluginRepositoriesId.setMoveable(true);
		tblclmnPluginRepositoriesId.setWidth(150);
		tblclmnPluginRepositoriesId.setText("ID");

		TableColumn tblclmnPluginRepositoriesName = new TableColumn(tablePluginRepositories, SWT.CENTER);
		tblclmnPluginRepositoriesName.setMoveable(true);
		tblclmnPluginRepositoriesName.setWidth(150);
		tblclmnPluginRepositoriesName.setText("Name");

		TableColumn tblclmnPluginRepositoriesUrl = new TableColumn(tablePluginRepositories, SWT.CENTER);
		tblclmnPluginRepositoriesUrl.setMoveable(true);
		tblclmnPluginRepositoriesUrl.setWidth(150);
		tblclmnPluginRepositoriesUrl.setText("URL");

		TableColumn tblclmnPluginRepositoriesLayout = new TableColumn(tablePluginRepositories, SWT.CENTER);
		tblclmnPluginRepositoriesLayout.setMoveable(true);
		tblclmnPluginRepositoriesLayout.setWidth(150);
		tblclmnPluginRepositoriesLayout.setText("Layout");
		compositePluginRepositories.setLayout(gl_compositePluginRepositories);

		listProfiles = new List(group, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);

		listProfiles.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = listProfiles.getSelectionIndex();
				if (index != -1) {
					openFrmProfileForEdit(shlMavenSettingsEditor, settings, index);
					btnProfileEdit.setEnabled(true);
					btnProfileDelete.setEnabled(true);
				} else {
					btnProfileEdit.setEnabled(false);
					btnProfileDelete.setEnabled(false);
				}
			}
		});
		listProfiles.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = listProfiles.getSelectionIndex();
				btnProfileEdit.setEnabled(index != -1);
				btnProfileDelete.setEnabled(index != -1);
				if (index == -1) {
					btnActiveByDefault.setSelection(false);
					textProfileJDK.setText("");
					textOsArch.setText("");
					textOsFamily.setText("");
					textOsName.setText("");
					textOsVersion.setText("");
					tableProperties.removeAll();
					tableRepositories.removeAll();
					tablePluginRepositories.removeAll();
				} else {
					Profile profile = settings.getProfiles().get(index);
					Activation activation = profile.getActivation();
					btnActiveByDefault.setSelection(activation.isActiveByDefault());
					textProfileJDK.setText(GuiUtils.formateNull(activation.getJdk()));
					textOsArch.setText(GuiUtils.formateNull(activation.getOs().getArch()));
					textOsFamily.setText(GuiUtils.formateNull(activation.getOs().getFamily()));
					textOsName.setText(GuiUtils.formateNull(activation.getOs().getName()));
					textOsVersion.setText(GuiUtils.formateNull(activation.getOs().getVersion()));

					GuiUtils.fillTablePropertiesItem(tableProperties, profile.getProperties());
					GuiUtils.fillTableRepositoriesItem(tableRepositories, profile.getRepositories());
					GuiUtils.fillTablePluginRepositoriesItem(tablePluginRepositories, profile.getPluginRepositories());
				}

			}
		});
		GroupLayout gl_group = new GroupLayout(group);
		gl_group.setHorizontalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(listProfiles, GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE));
		gl_group.setVerticalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(listProfiles, GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE));
		group.setLayout(gl_group);
		compositeProfiles.setLayout(gl_compositeProfiles);
		shlMavenSettingsEditor.setLayout(gl_shlMavenSettingsEditor);

		shlMavenSettingsEditor.open();
		shlMavenSettingsEditor.layout();

	}

	/**
	 * {@link FrmProfile}
	 * 
	 * @param parentShell
	 * @param settings
	 */
	private final void openFrmProfileForEdit(Shell parentShell, Settings settings, int index) {
		FrmProfile frmProfile = new FrmProfile(parentShell, settings.getProfiles());
		Profile oldProfile = settings.getProfiles().get(index);
		Profile profile = frmProfile.open(oldProfile);
		if (profile != null) {
			updateActiveProfiles(settings, oldProfile.getId(), profile.getId());
			settings.getProfiles().set(index, profile);
			GuiUtils.fillListProfileItem(listProfiles, settings.getProfiles());
		}
	}

	/**
	 * open {@link FrmServer}
	 * 
	 * @param parentShell
	 * @param settings
	 */
	private final void openFrmServersForEditing(int index, Shell parentShell, Settings settings) {
		FrmServer frmServers = new FrmServer(parentShell, settings.getServers());
		Server server = frmServers.open(settings.getServers().get(index));
		if (server != null) {
			settings.getServers().set(index, server);
			GuiUtils.fillTableItem(tableServers, settings.getServers());
		}
	}

	/**
	 * 
	 * open {@link FrmMirrors}
	 * 
	 * @param parentShell
	 * @param settings
	 * @param maven
	 * @param index
	 *            index of selected item from {@link #tableMirrors}
	 */
	private final void openFrmMirrorsForEditing(Shell parentShell, Settings settings, Maven maven, int index) {
		FrmMirrors frmMirrors = new FrmMirrors(parentShell, settings.getMirrors());
		Mirror mirror = frmMirrors.open(settings.getMirrors().get(index), maven.getVersion());
		if (mirror != null) {
			settings.getMirrors().set(index, mirror);
			GuiUtils.fillTableMirrorsItem(tableMirrors, settings.getMirrors());
		}
	}

	/**
	 * open {@link FrmProxy}
	 * 
	 * @param parentShell
	 * @param settings
	 */
	private final void openFrmProxiesForEditing(Shell parentShell, Settings settings, int index) {
		FrmProxy frmProxies = new FrmProxy(parentShell, settings.getProxies());
		Proxy proxy = frmProxies.open(settings.getProxies().get(index));
		if (proxy != null) {
			settings.getProxies().set(index, proxy);
			GuiUtils.fillTableProxiesItem(tableProxies, settings.getProxies());
		}
	}

	private final void updateActiveProfiles(Settings settings, String oldId, String newId) {
		for (int i = 0; i <= settings.getActiveProfiles().size() - 1; i++) {
			String activeProfile = settings.getActiveProfiles().get(i);
			if (activeProfile.equalsIgnoreCase(oldId)) {
				settings.getActiveProfiles().set(i, newId);
			}
		}
	}

	/**
	 * validate {@link Settings#getLocalRepository()}
	 * 
	 * @param settings
	 */
	private final void validateLocalRepository(Settings settings) {
		File file = new File(settings.getLocalRepository());
		if (!file.isDirectory()) {
			labelWarning.setText("Local repository is not directory");
		} else {
			labelWarning.setText("");
		}
	}

	/**
	 * open {@link FrmSelectProfile}
	 * 
	 * @param parentShell
	 * @param settings
	 */
	private final void openFrmReplaceActiveProfiles(Shell parentShell, Settings settings, int index) {
		FrmSelectProfile frmSelectProfile = new FrmSelectProfile(parentShell, settings.getProfiles(), settings.getActiveProfiles());
		String activeProfile = frmSelectProfile.open();
		if (activeProfile != null) {
			settings.getActiveProfiles().set(index, activeProfile);
			GuiUtils.fillListItem(listActiveProfiles, settings.getActiveProfiles());
		}
	}

	/**
	 * {@link FrmSelectPluginGroups}
	 * 
	 * @param parentShell
	 * @param settings
	 * @param edit
	 */
	private final void openFrmLoadingPluginGroups(Shell parentShell, Settings settings, int index, boolean edit) {
		FrmLoadingPluginGroups frmLoadingPluginGroups = new FrmLoadingPluginGroups(parentShell);
		java.util.List<String> list = frmLoadingPluginGroups.open(settings.getRepository());
		if (!frmLoadingPluginGroups.isCanceled()) {

			String pluginGroup = null;
			FrmSelectPluginGroups frmSelectPluginGroups = new FrmSelectPluginGroups(parentShell, settings.getPluginGroups());
			if (edit) {
				pluginGroup = frmSelectPluginGroups.open(list);
				if (pluginGroup != null) {
					settings.getPluginGroups().set(index, pluginGroup);
				}
			} else {
				pluginGroup = frmSelectPluginGroups.open(list);
				if (pluginGroup != null) {
					settings.addPluginGroups(pluginGroup);
				}
			}
			GuiUtils.fillListItem(listPluginGroups, settings.getPluginGroups());

		}
	}

	private final void removeProfileFromActive(Settings settings, String profileId) {
		for (int i = 0; i <= settings.getActiveProfiles().size() - 1; i++) {
			String activeProfileId = settings.getActiveProfiles().get(i);
			if (activeProfileId.equalsIgnoreCase(profileId)) {
				settings.getActiveProfiles().remove(i);
			}
		}
	}

	/**
	 * open window for maven
	 * 
	 * @param maven
	 */
	public final Settings open(Maven maven) {
		try {
			Settings settings = SettingsFactory.loadSettings(maven);

			createContents(settings, maven);

			while (!shlMavenSettingsEditor.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			MessageBox messageBox = new MessageBox(shlMavenSettingsEditor, SWT.ICON_ERROR);
			messageBox.setMessage("Failed parse settings.xml in " + maven.getHome().getPath() + File.separator + "conf" + File.separator + "settings.xml");
			messageBox.setText("Failed parse Settings.xml");
			shlMavenSettingsEditor.close();
		}
		return newSettings;
	}
}
